package com.ys.controller.web.finance;

import com.ys.config.MenuId;
import com.ys.dto.SubGridConfigDto;
import com.ys.entity.finance.TcwRoleCostApply;
import com.ys.entity.finance.TcwRoleCostApplyItem;
import com.ys.service.finance.TcwRoleCostApplyService;
import com.ys.service.flow.YspTaskService;
import com.ys.service.sys.CommonApiService;
import com.ys.vo.Flow;
import com.ys.vo.W;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.*;

/**
 * 职能费用申请
 */
@Controller
@RequestMapping("/web/finance/tcwRoleCostApply")
public class WebTcwRoleCostApplyController {
	private static final Logger logger = LoggerFactory.getLogger(WebTcwRoleCostApplyController.class);

	@Autowired
	private CommonApiService<TcwRoleCostApply> commonApiService;

    @Autowired
    private YspTaskService yspTaskService;

    @Autowired
    private TcwRoleCostApplyService tcwRoleCostApplyService;

    /*
     * 新增
     */
    @GetMapping("/toAdd")
    public String toAdd(Model model, HttpServletRequest request, HttpServletResponse response) {
        try {
            Map<String, Object> extraParam = new HashMap<>();
            //关联数据
            extraParam.put("status", 1);

            TcwRoleCostApply entity = new TcwRoleCostApply();
            //关联数据
            String bid = commonApiService.getCommonNextBid(MenuId.TCW_RoleCostApply);
            entity.setBid(bid);

            // 子列表配置信息
            SubGridConfigDto subGridConfig = commonApiService.getSubGrid(MenuId.TCW_RoleCostApply,new ArrayList<>());
            return commonApiService.toAdd(model, entity, MenuId.TCW_RoleCostApply, extraParam, subGridConfig);
        }catch (Exception e) {
            logger.error("新增页面出错：",e.getMessage(),e);
            return W.error(model, e.getMessage());
        }
    }

    /*
     * 编辑
     */
    @GetMapping("/toEdit")
    public String toEdit(Model model, HttpServletRequest request, HttpServletResponse response, String bid) {
        try {
            Map<String, Object> extraParam = tcwRoleCostApplyService.getExtraParam(bid);

            List<Map<String, Object>> itemList = tcwRoleCostApplyService.getItemList(bid);
            // 子列表配置信息
            SubGridConfigDto subGridConfig = commonApiService.getSubGrid(MenuId.TCW_RoleCostApply,itemList);

            //子表按钮
           // List<SubGridConfigDto.ButtonDto> buttonArr = subGridConfig.getButtonArr();
            Integer state = yspTaskService.flowState(bid);
            if (state == Flow.State.FINISH.value){
                subGridConfig.setButtonArr(new ArrayList<>());
            }else if (state == Flow.State.PROCESS.value){
                subGridConfig.setButtonArr(new ArrayList<>());
            }


            commonApiService.toEdit(model,Long.parseLong(extraParam.get("id").toString()), new TcwRoleCostApply(), MenuId.TCW_RoleCostApply, extraParam, subGridConfig);

            return "finance/tcwRoleCostApply/toEdit";
        }catch (Exception e) {
            logger.error("编辑页面出错：",e.getMessage(),e);
            return W.error(model, e.getMessage());
        }
    }


    /*
     * 新增，根据导入数据
     */
    @GetMapping("/toAddByImport")
    public String toAddByImport(Model model, HttpServletRequest request, HttpServletResponse response, String bid){
        try {
            //主表信息
            Map<String, Object> extraParam = tcwRoleCostApplyService.getExtraParam(bid);
            //子表信息
            List<Map<String, Object>> itemList = tcwRoleCostApplyService.getItemList(bid);
            // 子列表配置信息
            SubGridConfigDto subGridConfig = commonApiService.getSubGrid(MenuId.TCW_RoleCostApply,itemList);


            //子表按钮
            Integer state = yspTaskService.flowState(bid);
            if (state == Flow.State.FINISH.value){
                subGridConfig.setButtonArr(new ArrayList<>());
            }else if (state == Flow.State.PROCESS.value){
                subGridConfig.setButtonArr(new ArrayList<>());
            }

            TcwRoleCostApply entity = new TcwRoleCostApply()
                    .setBid(bid)
                    .setCreateTime(new Date());

            return commonApiService.toAdd(model,entity, MenuId.TCW_RoleCostApply, extraParam, subGridConfig);

        } catch (Exception e) {
            e.printStackTrace();
            return W.error(model, e.getMessage());
        }
    }





}






